package com.eleadmin.order.controller;

import com.eleadmin.common.core.web.*;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.order.entity.ContainerBill;
import com.eleadmin.order.entity.Driver;
import com.eleadmin.order.service.DriverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * 司机管理
 * @author : Doman
 * @date : 2023-04-07 13:41:05
 * @version ：1.0
 */

@Api(tags = "司机管理")
@RestController
@RequestMapping("/api/order/driver")
public class DriverController extends BaseController {

    @Autowired
    private DriverService driverService;

    @PreAuthorize("hasAuthority('order:driver:list')")
    @ApiOperation("分页查询司机管理")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, Driver driver) throws Exception{
        return driverService.list(page, driver, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:driver:list')")
    @ApiOperation("查询全部司机管理")
    @GetMapping()
    public JsonResult list(Driver driver){
        return driverService.list(driver, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:driver:list')")
    @ApiOperation("返回司机select列表")
    @GetMapping("listSelCarNo")
    public JsonResult listSelCarNo(Driver driver){
        return driverService.listSelCarNo(driver, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:driver:list')")
    @ApiOperation("返回司机select列表")
    @GetMapping("listSelDriverName")
    public JsonResult listSelDriverName(Driver driver){
        return driverService.listSelDriverName(driver, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:driver:list')")
    @ApiOperation("根据id查询司机管理")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return driverService.getById(id);
    }


    @PreAuthorize("hasAuthority('order:driver:save')")
    @OperationLog
    @ApiOperation(value ="添加司机管理")
    @PostMapping()
    public JsonResult save(@RequestBody Driver driver) {
        try {
            return driverService.save(driver, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }

    }

    @PreAuthorize("hasAuthority('order:driver:update')")
    @OperationLog
    @ApiOperation(value ="修改司机管理")
    @PutMapping()
    public JsonResult update(@RequestBody Driver driver) {
        try {
            return driverService.update(driver, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('order:driver:remove')")
    @OperationLog
    @ApiOperation(value ="删除司机管理")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        try {
            return driverService.remove(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }


    @PreAuthorize("hasAuthority('order:customer:exportsCustomer')")
    @ApiOperation("导出车队司机拖车账单")
    @GetMapping("/exportsDriver")
    public void exportsDriver(ContainerBill containerBill, HttpServletResponse response, HttpServletRequest request) throws Exception{
        driverService.exportsDriver(containerBill, getLoginUser(),response, request);
    }

}
